package com.zy.untils;

import android.content.Context;
import android.os.Environment;
import android.os.storage.StorageManager;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

/**
 *
 */
public class DeviceUtils {

    private static final String TAG = "DeviceUtils";

    /**
     * 检测当前设备是否有USB1的设备
     *
     * @return true：有；false：无
     */
    public static boolean checkUSBDevice(String usbPath) {
        Log.d(TAG, "checkUSBDevice: usbPath = " + usbPath);
        File file = new File(usbPath);
        String state = Environment.getExternalStorageState(file);
        Log.d(TAG, "checkUSBDevice: state = " + state);
        boolean exists = file.exists();
        Log.d(TAG, "checkUSBDevice: exists = " + exists);
        return exists;

    }

    //    public static String  getUsbStatus(Context mContext) {
//        StorageManager mStorageManager = (StorageManager) mContext
//                .getSystemService(Context.STORAGE_SERVICE);
//        String[] strS = mStorageManager.getVolumePaths();
//        String path="";
//        for (String str : strS) {
//            switch (str) {
//                case "/storage/usb0":
//                    path="/storage/usb0";
//                    break;
//                case "/storage/usb2":
//                    path="/storage/usb2";
//                    break;
//            }
//        }
//        LogUtil.v(TAG,"u:地址： "+path);
//        return path;
//    }
    public static String haveUsb(Context context)   {
        String path="";
        StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
        try {
            Class storeManagerClazz = Class.forName("android.os.storage.StorageManager");

            Method getVolumesMethod = storeManagerClazz.getMethod("getVolumes");

            List<?> volumeInfos = (List<?>) getVolumesMethod.invoke(storageManager);

            Class volumeInfoClazz = Class.forName("android.os.storage.VolumeInfo");

            Method getTypeMethod = volumeInfoClazz.getMethod("getType");
            Method getFsUuidMethod = volumeInfoClazz.getMethod("getFsUuid");
            Method getPath = volumeInfoClazz.getMethod("getPath");

//            @SuppressLint("SoonBlockedPrivateApi")
//            Field fsTypeField = volumeInfoClazz.getDeclaredField("fsType");
//            Field fsLabelField = volumeInfoClazz.getDeclaredField("fsLabel");
//            Field pathField = volumeInfoClazz.getDeclaredField("path");
//            Field internalPath = volumeInfoClazz.getDeclaredField("internalPath");
            if (volumeInfos != null) {//   /storage/usb2
                for (Object volumeInfo : volumeInfos) {
                    String uuid = (String) getFsUuidMethod.invoke(volumeInfo);
                    File    file = (File) getPath.invoke(volumeInfo);
                    String path1 = file.getPath();
                  if(path1.contains("/storage/usb")){
                      return path1;
                  }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return path;
    }


    public static String SD = "内部存储";
    public static String EXT = "SD";
    public static String USB = "盘";
    public static String getStoragePath(Context mContext, String keyword) {
        String resultpath = "";
        StorageManager mStorageManager = (StorageManager) mContext
                .getSystemService(Context.STORAGE_SERVICE);
        Class<?> storageVolumeClazz = null;
        try {
            storageVolumeClazz = Class.forName("android.os.storage.StorageVolume");

            Method getVolumeList = mStorageManager.getClass().getMethod("getVolumeList");

            Method getPath = storageVolumeClazz.getMethod("getPath");



            Object result = getVolumeList.invoke(mStorageManager);

            final int length = Array.getLength(result);

            Method getUserLabel = storageVolumeClazz.getMethod("getUserLabel");

            for (int i = 0; i < length; i++) {

                Object storageVolumeElement = Array.get(result, i);


                String userLabel = (String) getUserLabel.invoke(storageVolumeElement);

// content += userLabel + "<><> " + "\r\n";

                String path = (String) getPath.invoke(storageVolumeElement);

                if(userLabel.contains(keyword)){
                    resultpath = path;
                }
// content += path + "<><> " + userLabel + "\r\n";

// boolean removable = (Boolean) isRemovable.invoke(storageVolumeElement);
//
// if (is_removale == removable) {
//
// return path;
// }
            }
// writeFile(content);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return resultpath;
    }

//    /**
//     * 设置系统亮度
//     * @param brightness
//     */
// public static void setBrightness(int brightness){
//     ContentResolver contentResolver = MyApplication.getContext().getContentResolver();
//     Settings.System.putInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS, brightness);
// }
//
//    /**
//     * 设置系统声音
//     * @param voice
//     */
//    public static void setVoice(int voice){
//        AudioManager mAudioManager = (AudioManager)  MyApplication.getContext().getSystemService(Context.AUDIO_SERVICE);
//        mAudioManager.setStreamVolume(AudioManager.STREAM_SYSTEM, voice, 0);
////        mAudioManager.setStreamVolume(type, volume, 0);
//    }

//这个写文件的方法是测试的时候将反射获取的外置路径以及外置卡信息写入文件中方便我们查看

    public static void writeFile(String content) {
        try {
            PrintStream ps = new PrintStream(new FileOutputStream(new File(
                    "/mnt/sdcard/path.txt")));
            ps.append(content);// 往文件里写入字符串
            ps.flush();
            ps.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
